// pages/perCenter/shipAddr/shipAddr.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addrs: [],
    winHeight: "",
    type:"",
    userList: [{ name: "", tel: "", idCard: "" }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      type: options.type
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    this.getData(that);
  },
  getData: function (that) {

    util.request(api.AddrList, { token: wx.getStorageSync("token") }).then(function (res) {
      if (res.code == '200') {
        that.setData({
          addrs: res.datas
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  fnToNewAddr: function () {
    wx.navigateTo({
      url: '/pages/perCenter/shipAddW/shipAddW',
    })
  },

  fnEdit: function (e) {
    var id = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    var mobile = e.currentTarget.dataset.mobile
    var province = e.currentTarget.dataset.province
    var city = e.currentTarget.dataset.city
    var county = e.currentTarget.dataset.county
    var detail = e.currentTarget.dataset.detail
    var isdefault = e.currentTarget.dataset.isdefault
    wx.navigateTo({
      url: '/pages/perCenter/shipAddW/shipAddW?id=' + id + "&name=" + name + "&mobile=" + mobile + "&province=" + province + "&city=" + city + "&county=" + county + "&detail=" + detail + "&isdefault=" + isdefault
    })
  },
  fnDel: function (e) {
    var that =this
    var id = e.currentTarget.dataset.id
    var obj = new Object();
    obj.id = id;
    wx.showLoading({
      title: '请稍候',
    })
    util.request(api.AddrOper, { token: wx.getStorageSync("token"), address: obj, type: "del" }).then(function (res) {
      wx.hideLoading()
      if (res.code == '200') {
        that.getData(that)
      }
    });
  },
  updateDefault: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var isdefault = e.currentTarget.dataset.isdefault
    if (isdefault == "true") {
      wx.showToast({
        title: '不能取消默认',
      })
      return;
    }
    var obj = new Object();
    obj.id = id;
    obj.isDefault = true;
    wx.showLoading({
      title: '请稍候',
    })
    util.request(api.AddrOper, { token: wx.getStorageSync("token"), address: obj, type: "edit" }).then(function (res) {
      wx.hideLoading()
      if (res.code == '200') {
        that.getData(that)
      }
    });
  },
  chooseAddress: function (e) {
    if (this.data.type == "choose") {

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    var item = e.currentTarget.dataset.item
    var remark1 = item.province+item.city+item.county
    var remark2 = item.detail 
    var mobile = item.mobile 
    var name = item.name 
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    this.data.userList[0].name = name
    this.data.userList[0].tel = mobile
    prevPage.setData({
      remark1: remark1,
      remark2: remark2,
      userList: this.data.userList
    });
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
      success: function (res) {
        // success
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })
  }}
})